home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / comm / net / grapevine.lha / Grapevine / irclink.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1994-10-27  |  7KB  |  278 lines

  1. #!/bin/sh
  2. #
  3. # execute this to create an "irclink" executable in the current
  4. # directory for your UNIX system.
  5. #
  6. # defaults to "cc", change this if you must :-)
  7. ccomp=cc
  8. # Remove -s if you do not want to strip the binary
  9. cflags=-s
  10.  
  11. cat > irclink-out.c << 'EOFIRCLINK'
  12. /*
  13.  * irclink.c  -- UNIX-side serial support for Grapevine
  14.  *      
  15.  * define CF_SGTTY for sgtty systems
  16.  */
  17.  
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netdb.h>
  23. #ifdef AIX
  24. #include <sys/select.h>
  25. #endif
  26. #ifdef CF_SGTTY
  27. #include <sgtty.h>
  28. #else
  29. #include <termios.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <signal.h>
  34.  
  35. #ifndef INADDR_NONE
  36. #define INADDR_NONE -1
  37. #endif
  38.  
  39. #define BUFSIZE 32
  40.  
  41. #ifdef CF_SGTTY
  42. #ifndef FREAD
  43. #define FREAD 0x0001
  44. #endif
  45. #ifndef FWRITE
  46. #define FWRITE 0x0002
  47. #endif
  48. struct sgttyb  ostty,    nstty;
  49. struct tchars  otchars,  ntchars;
  50. struct ltchars oltchars, nltchars;
  51. int            olmode,   nlmode;      
  52. int            inout = FREAD|FWRITE;  
  53. #else
  54. struct termios temp_tios;
  55. #endif
  56.  
  57. int temp;
  58.  
  59. int CleanUp( i )
  60. int i;
  61. {
  62.     if( temp )
  63.         {
  64. #ifdef CF_SGTTY
  65.         (void)ioctl(0, (int)TIOCFLUSH, (char *)&inout);
  66.         (void)ioctl(0, (int)TIOCSETP, (char *)&ostty);
  67.         (void)ioctl(0, (int)TIOCSETC, (char *)&otchars);
  68.         (void)ioctl(0, (int)TIOCSLTC, (char *)&oltchars);
  69.         (void)ioctl(0, (int)TIOCLSET, (char *)&olmode);
  70. #else
  71.         tcsetattr( 0, TCSANOW, &temp_tios );
  72. #endif
  73.         }
  74.     fcntl( 0, F_SETFL, fcntl( 0, F_GETFL, 0 ) & ~O_NDELAY );
  75.     exit( 0 );
  76. }
  77.  
  78. int HandleIO( fd )   /* Shuffle data between fd and stdin/stdout */
  79. int fd;
  80. {
  81.     int result, i;
  82.     char inbuf[BUFSIZE];
  83.     struct fd_set rd;
  84.     struct fd_set zer;
  85. #ifndef CF_SGTTY
  86.     struct termios tios;
  87. #endif
  88.     struct timeval timeout;
  89.  
  90.     signal( SIGINT,  (void *) CleanUp );    /* Clean up sockets when killed */
  91.     signal( SIGTSTP, (void *) CleanUp );
  92.     signal( SIGSEGV, (void *) CleanUp );
  93.     signal( SIGBUS,  (void *) CleanUp );
  94.     signal( SIGQUIT, (void *) CleanUp );
  95.     signal( SIGKILL, (void *) CleanUp );
  96.  
  97. #ifdef CF_SGTTY
  98.         ioctl(0, (int)TIOCGETP, (char *)&ostty);
  99.         ioctl(0, (int)TIOCGETC, (char *)&otchars);
  100.         ioctl(0, (int)TIOCGLTC, (char *)&oltchars);
  101.         ioctl(0, (int)TIOCLGET, (char *)&olmode);
  102.         nstty              = ostty;
  103.         ntchars            = otchars; 
  104.         nltchars           = oltchars;
  105.         nlmode             = olmode;  
  106.  
  107.  
  108.         nstty.sg_flags  = RAW;     /* | = RAW;  nstty.sg_flags &= ~ECHO; */
  109.  
  110.         nstty.sg_erase     = nstty.sg_kill     = -1;
  111.  
  112.         ntchars.t_intrc    = ntchars.t_quitc   = -1;
  113.         ntchars.t_eofc     = ntchars.t_brkc    = -1;
  114.  
  115.         nltchars.t_suspc   = nltchars.t_dsuspc = -1;
  116.       nltchars.t_rprntc  = nltchars.t_flushc = -1;
  117.         nltchars.t_werasc  = nltchars.t_lnextc = -1;
  118.  
  119.         /* tell ourselves about it */ 
  120.         ioctl(0, (int)TIOCSETN, (char *)&nstty);
  121.         ioctl(0, (int)TIOCSETC, (char *)&ntchars);
  122.         ioctl(0, (int)TIOCSLTC, (char *)&nltchars);
  123.         ioctl(0, (int)TIOCLSET, (char *)&nlmode);
  124. #else
  125.     tcgetattr( 0, &tios );        /* Get attributes for stdin                */
  126.     temp_tios = tios;            /* Make copy of tios for restoration    */
  127.     temp =1;
  128.     tios.c_lflag &= ~ICANON;    /* Disable canonical input              */
  129.     tios.c_cc[VMIN] = 1;        /* Read at least 1 character            */
  130.     tios.c_cc[VTIME] = 0;        /* Disable timeout                        */
  131.     tios.c_lflag &= ~ECHO;        /* No echo                                */
  132.     tios.c_lflag &= ~ISIG;        /* Disable input signals                */
  133.     tios.c_cflag &= ~CSIZE;
  134.     tios.c_cflag |=  CS8;        /* 8 bit connection                        */
  135.     tios.c_cflag &= ~PARENB;    /* No output parity                        */
  136.     tios.c_iflag &= ~INPCK;        /* No input parity                        */
  137.     tios.c_iflag &= ~ISTRIP;    /* Do not strip 8th bit                    */
  138.     tios.c_iflag &= ~IXON;        /* Disable XON/XOFF                        */
  139.     tios.c_iflag &= ~IXOFF;
  140.     tios.c_iflag &= ~INLCR;        /* No NL -> CR remapping                */
  141.     tios.c_iflag &= ~ICRNL;        /* No NL -> CR remapping                */
  142.     tios.c_oflag &= ~OPOST;        /* Disable output post processing        */
  143.  
  144.     tcsetattr( 0, TCSANOW, &tios );
  145.  
  146.     timeout.tv_sec = 1;
  147.     timeout.tv_usec = 0;
  148. #endif
  149.  
  150.     fcntl( 0, F_SETFL, O_NDELAY );    /* Disable read blocking */
  151.     fcntl( fd, F_SETFL, O_NDELAY );
  152.     while( 1 )
  153.     {
  154.         FD_ZERO( &rd );
  155.         FD_SET( 0, &rd );    /* Determine whether 0 can be read    */
  156.         FD_SET( fd, &rd );    /* Determine whether fd can be read    */
  157.         FD_ZERO( &zer );    /* No write or exception queries    */
  158.  
  159.         if( select( NFDBITS, &rd, &zer, &zer, &timeout ) != -1 )
  160.         {
  161.             if( FD_ISSET( 0, &rd ) )
  162.             {  /* read zero */
  163.                 result = read( 0, inbuf, BUFSIZE );
  164.                 if( result > 0 )
  165.                 {
  166.                     write( fd, inbuf, result );
  167.                 }
  168.                 if( result < 0 )
  169.                 {
  170.                     printf( "\n\nEOF detected, exiting.\n" );
  171.                     CleanUp( 0 );
  172.                 }
  173.             }
  174.             if( FD_ISSET( fd, &rd ) )
  175.             {
  176.                 result = read( fd, inbuf, BUFSIZE );
  177.                 if( result > 0 )
  178.                     write( 1, inbuf, result );
  179.                 if( result < 0 )
  180.                 {
  181.                     printf( "\n\nERROR: Connection to server lost, quitting.\n" );
  182.                     CleanUp(0);
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189. int ConnectToServer( servername, port )
  190. char *servername;
  191. int port;
  192. {
  193.     int sock;
  194.     struct hostent *host;
  195.     struct sockaddr_in serv;
  196.  
  197.     printf( "Connecting to server %s port %ld.\n", servername, port );
  198.  
  199.     sock = socket( AF_INET,SOCK_STREAM,0 );
  200.     if( sock < 0 )
  201.     {
  202.         fprintf( stderr, "irclink: Could not get socket.\n" );
  203.         return( -1 );
  204.     }
  205.  
  206.     serv.sin_addr.s_addr = inet_addr( servername );
  207.     serv.sin_port = htons( port );
  208.     serv.sin_family = AF_INET;
  209.  
  210.     if( serv.sin_addr.s_addr == INADDR_NONE )
  211.     {
  212.         if( host = (struct hostent *) gethostbyname( servername ) )
  213.         {
  214.             memcpy( &serv.sin_addr, host->h_addr, host->h_length );
  215.         }
  216.         else
  217.         {
  218.             fprintf( stderr, "irclink: Unknown host.\n" );
  219.             return( -1 );
  220.         }
  221.     }
  222.  
  223.     if( connect( sock, (struct sockaddr *)&serv, sizeof( struct sockaddr_in ) ) == -1)
  224.         return( -1 );
  225.     else
  226.         return( sock );
  227. }
  228.  
  229. main( argc, argv )
  230. int argc;
  231. char **argv;
  232. {
  233.     char buf[2];
  234.     char *cmdarg;
  235.     char *servername;
  236.     int sock;
  237.     int port;
  238.     port = 6667;
  239.  
  240.     temp = 0;
  241.  
  242.     if( argv[1] )
  243.         servername = argv[1];
  244.     else
  245.     {
  246.         printf( "Usage 2: %s server [port]\n", argv[0] );
  247.         exit( 1 );
  248.     }
  249.  
  250.     if( argv[2] )
  251.         port = atoi( argv[2] );
  252.  
  253.     sock = ConnectToServer( servername, port );
  254.  
  255.     if( sock < 0 )
  256.     {
  257.         fprintf( stderr, "irclink: Could not connect to server.\n" );
  258.         exit( 5 );
  259.     }
  260.  
  261.     printf( "Connected to server %s.\n", servername );
  262.     HandleIO( sock );
  263.     CleanUp( 0 );    /* make sure to clean up */
  264.     return( 0 );
  265. }
  266. EOFIRCLINK
  267.  
  268. if [ -f /usr/include/termios.h ] ; then
  269.     if [ -f /usr/include/sys/aixgsc.h ] ; then           
  270.         $ccomp -DAIX -o irclink irclink-out.c $cflags
  271.     else                                                 
  272.         $ccomp -o irclink irclink-out.c $cflags      
  273.     fi
  274. else
  275.     $ccomp -DCF_SGTTY -o irclink irclink-out.c $cflags
  276. fi
  277. rm irclink-out.c
  278.